package com.deli.core.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.SortClause;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.deli.core.pojo.SuperPojo;
import com.deli.core.tools.PageHelper.Page;

@Service("solrService")
public class SolrServiceImpl implements SolrService {

	@Autowired
	private HttpSolrServer solrServer;

	@Override
	public Page<SuperPojo> toSolrSearchKeyword(String keyword, String sort,
			Long brandId, Float pa, Float pb, Integer pageNum,
			Integer pageSize) throws SolrServerException {
		// solr,一般都是查询商品的名称作为查询条件
		// 设置查询的条件
		// 依据name_ik查询
		SolrQuery solrQuery = new SolrQuery("name_ik:" + keyword);
		//追加查询品牌的id查询
		if (brandId != null) {
			solrQuery.addFilterQuery("brandId:"+brandId);
		}
		//追加查询依据价格查询
		if (pa!=null&&pb!=null) {
			if(pb==-1){
				solrQuery.addFilterQuery("price:["+pa+" TO *]");
			}else{
				solrQuery.addFilterQuery("price:["+pa+" TO "+pb+"]");
			}
		}
		// 设置高亮
		solrQuery.setHighlight(true);
		solrQuery.addHighlightField("name_ik");
		solrQuery.setHighlightSimplePre("<span style='color:red'>");
		solrQuery.setHighlightSimplePost("</span>");
		// 设置排序
		if (sort != null && sort.trim().length() > 1) {
			SortClause sortClause = new SortClause(sort.split(" ")[0],
					sort.split(" ")[1]);
			solrQuery.setSort(sortClause);
		}

		// 设置分页的起始页和一页显示的行数
		Page page = new Page(pageNum, pageSize);
		solrQuery.setStart(page.getStartRow());
		solrQuery.setRows(page.getPageSize());
		// 查询
		QueryResponse response = solrServer.query(solrQuery);
		// 设置高亮的集合
		// 大map的key是商品的id,小map的key是name_ik的列名,list是具体的数据
		Map<String, Map<String, List<String>>> highlighting = response
				.getHighlighting();
		// 查询之后得到结果集
		SolrDocumentList results = response.getResults();
		long numFound = results.getNumFound();
		// 设置page的总页数
		page.setTotal(numFound);
		List<SuperPojo> list = new ArrayList<SuperPojo>();
		for (SolrDocument solr : results) {
			String id = (String) solr.get("id");
			Map<String, List<String>> map = highlighting.get(id);
			String name = map.get("name_ik").get(0);
			// String name = (String) solr.get("name_ik");
			Long brandId2 = (Long) solr.get("brandId");
			Float price = (Float) solr.get("price");
			String url = (String) solr.get("url");
			SuperPojo superPojo = new SuperPojo();
			superPojo.setProperty("id", id);
			superPojo.setProperty("name", name);
			superPojo.setProperty("brandId", brandId2);
			superPojo.setProperty("price", price);
			superPojo.setProperty("url", url);
			list.add(superPojo);
		}
		page.setResult(list);
		return page;
	}

}
